package com.zzmm.socket.demo;

import lombok.SneakyThrows;
import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class SocketTCPQQClientTest {

    String dir = "src/main/resources/";

    @SneakyThrows
    @Test
    public void testSocketClient01() {
        InetAddress inetAddress = InetAddress.getByName("192.168.43.181");
        Socket socket = new Socket(inetAddress, 9999);
        System.out.println("socket = " + socket);

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(("Hello Server").getBytes(StandardCharsets.UTF_8));

        Thread.sleep(1000);
        System.out.println("睡醒了");

        byte[] data = new byte[1024];
        int len;
        InputStream inputStream = socket.getInputStream();
        len = inputStream.read(data);
        System.out.println(new String(data, 0, len));
        //
        // socket.close();
    }

    @SneakyThrows
    @Test
    public void testSocketClient02() {
        InetAddress inetAddress = InetAddress.getByName("192.168.43.181");
        Socket socket = new Socket(inetAddress, 9999);
        System.out.println("创建socket与服务端通讯 " + socket);

        // 向服务端发送文件
        System.out.println("客户端正在发送文件");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(dir + "img.png"));
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(socket.getOutputStream());
        byte[] data = new byte[1024];
        int len;
        while ((len = bufferedInputStream.read(data)) != -1) {
            bufferedOutputStream.write(data, 0, len);
        }
        bufferedInputStream.close();
        socket.shutdownOutput();

        // 读取服务端回复，确认文件已被接收
        System.out.println("客户端正在读取服务端回复，确认文件已被接收");
        bufferedInputStream = new BufferedInputStream(socket.getInputStream());
        while ((len = bufferedInputStream.read(data)) != -1) {
            System.out.println(new String(data, 0, len));
        }

        socket.close();
    }

    @SneakyThrows
    @Test
    public void testSocketClient03() {
        InetAddress inetAddress = InetAddress.getByName("192.168.43.181");
        Socket socket = new Socket(inetAddress, 9999);
        System.out.println("创建socket与服务端通讯 " + socket);

        byte[] data = new byte[1024];
        int len;
        InputStream inputStream = socket.getInputStream();
        while ((len = inputStream.read(data)) != -1) {
            System.out.println(new String(data, 0, len));
        }

        socket.close();
    }

    @SneakyThrows
    @Test
    public void testSocketClient04() {
        InetAddress inetAddress = InetAddress.getByName("192.168.43.181");
        Socket socket = new Socket(inetAddress, 9999);
        System.out.println("创建socket与服务端通讯 " + socket);

        byte[] data = new byte[1024];
        int len;
        InputStream inputStream = socket.getInputStream();
        while ((len = inputStream.read(data)) != -1) {
            System.out.println(new String(data, 0, len));
        }

        socket.close();
    }

}
